package com.cloudkinto.common.common;

import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/*******************************************************************
 * 类描述：
 *
 * @author: weijun
 * @date： 2018/4/25 12:40
 * ${TAGS}
 * @version 1.0
 *
 *
 * Version    Date       ModifiedBy                 Content
 * -------- ---------    ----------         ------------------------
 * 1.0      2018/4/25     weijun
 *
 *******************************************************************
 */
public class RandomGenerator {
    /***/
    private static final char[] DEFAULT_CODEC = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();

    private static final char[] NUMBER_CODEC = "1234567890".toCharArray();

    /**
     * Random
     */
    private Random random = new SecureRandom();

    private Random numberRandom = new Random();

    /**
     * Length of Random
     */
    private int length;

    /**
     * 构造方法
     */
    public RandomGenerator() {
        this(6);
    }

    /**
     * 构造方法
     *
     * @param length
     */
    public RandomGenerator(int length) {
        this.length = length;
    }

    /**
     * 生成随机字符串
     *
     * @return
     */
    public String generate() {
        byte[] verifierBytes = new byte[length];
        random.nextBytes(verifierBytes);
        return getAuthorizationCodeString(verifierBytes);
    }


    /**
     * 生成随机字符串
     *
     * @return
     */
    public String generateNum() {
        byte[] verifierBytes = new byte[length];
        random.nextBytes(verifierBytes);
        return getNumCodeString(verifierBytes);
    }

    /**
     * 生成授权码
     *
     * @param verifierBytes
     * @return
     */
    protected String getAuthorizationCodeString(byte[] verifierBytes) {
        char[] chars = new char[verifierBytes.length];
        for (int i = 0; i < verifierBytes.length; i++) {
            chars[i] = DEFAULT_CODEC[((verifierBytes[i] & 0xFF) % DEFAULT_CODEC.length)];
        }
        return new String(chars);
    }


    /**
     * 生成授权码
     *
     * @param verifierBytes
     * @return
     */
    protected String getNumCodeString(byte[] verifierBytes) {
        char[] chars = new char[verifierBytes.length];
        for (int i = 0; i < verifierBytes.length; i++) {
            chars[i] = NUMBER_CODEC[((verifierBytes[i] & 0xFF) % NUMBER_CODEC.length)];
        }
        return new String(chars);
    }

    public String getVolumeBatchString(Long shopId) {
        Date nowTime = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String resultStr = sdf.format(nowTime);
        resultStr = resultStr + shopId + generate();//规则时间+店铺id+随机字符串
        return resultStr;
    }

    /**
     * Setter for Random
     *
     * @param random
     */
    public void setRandom(Random random) {
        this.random = random;
    }

    /**
     * Setter for length
     *
     * @param length
     */
    public void setLength(int length) {
        this.length = length;
    }

    /**
     * 生成0-100中间的随机两位数字
     *
     * @return
     */
    public String get2randomNumber() {
        int ends = random.nextInt(99);
        String result = String.format("%02d", ends);
        return result;
    }

    public String get2Number(Integer orderNumber) {
        String result = String.format("%02d", orderNumber);
        return result;
    }

    public String get3Number(Integer orderNumber) {
        String result = String.format("%03d", orderNumber);
        return result;
    }

    public String get4Number(Integer orderNumber) {
        String result = String.format("%04d", orderNumber);
        return result;
    }

    public String get5Number(Integer orderNumber) {
        String result = String.format("%05d", orderNumber);
        return result;
    }


}
